/**
 * Created by liaoxd on 2017/11/17.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/messageManager/messageManager.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('MESSAGE_LEVEL,MESSAGE_TYPE');
    // 创建virtual filter model
    var vm = yufp.custom.vue({
      // TODO 请替换此id属性
      el: cite.el,
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;
        return {
          formdata: {},
          serviceUrl: backend.appCommonService + '/api/adminsmmessage/',
          idView: false,
          height: yufp.custom.viewSize().height - 140,
          userId: yufp.session.userId,
          tableColumns: [
            { label: this.$t('messageManager.xxm'), prop: 'code', width: 100, sortable: true, resizable: true },
            { label: this.$t('messageManager.tsnr'), prop: 'message', minWidth: 220 },
            { label: this.$t('messageManager.xxjb'), prop: 'messageLevel', width: 100, type: 'select', dataCode: 'MESSAGE_LEVEL' },
            { label: this.$t('messageManager.xxlb'), prop: 'messageType', width: 100, type: 'select', dataCode: 'MESSAGE_TYPE' },
            { label: this.$t('messageManager.ssmkmc'), prop: 'funcName'},
            { label: this.$t('messageManager.zxbgyh'), prop: 'lastChgUsr'},
            { label: this.$t('messageManager.zxbgsj'), prop: 'lastChgDt'}

          ],
          updateFields: [{
            columnCount: 1,
            fields: [
              { field: 'code', label: this.$t('messageManager.xxm'), rules: [{ required: true, message: this.$t('messageManager.btx'), trigger: 'blur' }] },
              { field: 'message', label: this.$t('messageManager.tsnr'), rules: [{ required: true, message: this.$t('messageManager.btx'), trigger: 'blur' }] },
              { field: 'messageLevel', label: this.$t('messageManager.xxjb'), type: 'select', dataCode: 'MESSAGE_LEVEL', rules: [{ required: true, message: this.$t('messageManager.btx'), trigger: 'blur' }] },
              { field: 'messageType', label: this.$t('messageManager.xxlb'), type: 'select', dataCode: 'MESSAGE_TYPE', rules: [{ required: true, message: this.$t('messageManager.btx'), trigger: 'blur' }] },
              { field: 'funcName', label: this.$t('messageManager.ssmkmc') }
            ]
          }],
          updateButtons: [
            {
              label: this.$t('messageManager.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model, valid) {
                if (valid) {
                  model.lastChgUsr = me.userId;
                  me.createFilter(model);
                }
              }
            },
            {
              label: this.$t('messageManager.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model, valid) {
                if (valid) {
                  model.lastChgUsr = me.userId;
                  me.dataFliterEditFn(model);
                  me.dialogVisible = false;
                  me.$refs.filterTable.remoteData();
                }
              }
            },
            {
              label: this.$t('messageManager.qx'),
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
              }
            }
          ],
          // 表单是否显示
          dialogVisible: false,
          // 表单是否可用
          formDisabled: false,
          // 表单操作状态
          viewType: 'DETAIL',

          filterGrid: {
            // 系统参数当前行
            currentRow: null,
            // 系统参数多选ID
            multipleSelection: '',
            data: null,
            subdata: null,
            total: null,
            loading: true,
            subloading: true,

            paging: {
              page: 1,
              pageSize: 10
            },
            // 系统参数模糊查询表头
            query: {
              instuId: '',
              propDesc: '',
              propName: ''
            }

          }
        };
      },
      methods: {
        rowClickFn: function (selection, row) {
          this.selections = selection;
          // 用于单个修改
          this.filterGrid.currentRow = row;
          if (selection.length > 0) {
            for (var i = 0; i < selection.length; i++) {
              // 记录多选用于多删
              if (selection.length == 1) {
                this.filterGrid.multipleSelection = selection[i].messageId;
              } else {
                this.filterGrid.multipleSelection = this.filterGrid.multipleSelection + ',' + selection[i].messageId;
              }
            }
          }
        },

        // 编辑按钮
        lookuptableEditFn: function () {
          if (this.$refs.filterTable.selections.length < 1) {
            vm.$message({ message: vm.$t('messageManager.qxzytjlxg') });
            return false;
          }
          if (this.$refs.filterTable.selections.length > 1) {
            vm.$message({ message: vm.$t('messageManager.znxzytjlxg') });
            return false;
          }

          this.viewType = 'UPDATE';
          vm.dialogVisible = true;
          this.$nextTick(function () {
            yufp.extend(this.$refs.datafilterForm.formModel, this.$refs.filterTable.selections[0]);
            this.$refs.datafilterForm.rebuildFn();
            this.$refs.datafilterForm.switch('code', 'disabled', true);
          });
        },


        // 修改系统参数
        dataFliterEditFn: function (row) {
          this.filterGrid.currentRow = row;

          yufp.service.request({
            url: backend.appCommonService + '/api/adminsmmessage/updates',
            method: 'post',
            data: this.filterGrid.currentRow,
            callback: function (code, message, response) {
              if (code == '0') {
                vm.$message({ message: vm.$t('messageManager.xgcg') });

                vm.$refs.filterTable.remoteData();
              } else {
                vm.$message({ message: vm.$t('messageManager.xgsb') });
              }
            }
          });
        },

        // 增加系统参数空行
        addFilterRecord: function () {
          this.dialogVisible = true;
          this.viewType = 'ADD';

          this.$nextTick(function () {
            this.$refs.datafilterForm.resetFn();
            this.$refs.datafilterForm.switch('code', 'disabled', false);
          });
        },

        // 保存新增系统参数
        createFilter: function (row) {
          this.filterGrid.currentRow = row;
          delete this.filterGrid.currentRow.messageId;
          yufp.service.request({
            url: backend.appCommonService + '/api/adminsmmessage/createvalidate',
            method: 'post',
            data: this.filterGrid.currentRow,
            callback: function (code, message, response) {
              if (code == '0') {
                if (response.code == '0') {
                  vm.$message({ message: vm.$t('messageManager.bccg') });
                  vm.$refs.filterTable.remoteData();
                  vm.dialogVisible = false;
                } else {
                  vm.$message({ message: response.message, type: 'warning' });
                  return false;
                }
              } else {
                vm.$message({ message: vm.$t('messageManager.bcsb') });
              }
            }
          });
        },

        // 批量删除系统参数
        dataFiltermultDeleteFn: function () {
          var ids = '';
          var filterSelecttions = this.$refs.filterTable.selections;
          if (filterSelecttions.length > 0) {
            for (var i = 0; i < filterSelecttions.length; i++) {
              // 记录多选用于多删
              if (filterSelecttions.length === 1) {
                ids = filterSelecttions[i].code;
              } else {
                ids = ids + ',' + filterSelecttions[i].code;
              }
            }
          } else {
            vm.$message({ message: vm.$t('messageManager.qxzxyscdxtxx') });
            return false;
          }
          vm.$confirm(vm.$t('messageManager.qrplscxttsxxpz'), vm.$t('messageManager.ts'), {
            confirmButtonText: vm.$t('messageManager.qd'),
            cancelButtonText: vm.$t('messageManager.qx'),
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              url: backend.appCommonService + '/api/adminsmmessage/deletes/' + ids,
              method: 'post',
              data: { ids: ids },
              callback: function (code, message, response) {
                if (code == '0') {
                  vm.$message({ message: vm.$t('messageManager.sccg') });
                  vm.$refs.filterTable.remoteData();
                } else {
                  // alert('删除失败！');
                  vm.$message({ message: vm.$t('messageManager.scsb') });
                }
              }
            });
          });
        }


      },
      filters: {

      },
      watch: {
        viewType: function (value) {
          if (value == 'ADD') {
            this.updateButtons[0].hidden = false;
            this.updateButtons[1].hidden = true;
          } else if (value == 'UPDATE') {
            this.updateButtons[0].hidden = true;
            this.updateButtons[1].hidden = false;
          } else if (value == 'DETAIL') {
            this.updateButtons[0].hidden = true;
            this.updateButtons[1].hidden = true;
          }
        }

      }
    });
  };


  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
